package com.turing.post.organization.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.organization.util.MerchantFeeSettlePolicyFloat;
import com.turing.post.organization.util.MerchantFeeSettlePolicyFloatDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.organization.util.OrganRespCode;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Jan 3, 2013
 */

@Stateless(mappedName = "organization.impl.MerchantFeeSettlePolicyFloatDaoImpl")
@Remote
@Local
public class MerchantFeeSettlePolicyFloatDaoImpl implements
		MerchantFeeSettlePolicyFloatDao {
	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void deleteMerchantFeeSettlePolicyFloatByFeePolicyId(
			BigInteger feePolicyId) throws PCPSApplicationException {
		String sql = "delete from MerchantFeeSettlePolicyFloat where FeePolicyId="
				+ feePolicyId;
		Query query = _em.createNativeQuery(sql);
		try {
			query.executeUpdate();
		} catch (Exception e) {
			throw new OrganApplicationException(
					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
					OrganRespCode.ISSUE_INSERT_UPDATE,
					"删除商户手续费收入结算策略浮动失败，编号已被引用!");
		}
	}

	@Override
	public MerchantFeeSettlePolicyFloat getMerchantFeeSettlePolicyFloatByFeePolicyId(
			BigInteger feePolicyId) {
		return _em.find(MerchantFeeSettlePolicyFloat.class, feePolicyId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MerchantFeeSettlePolicyFloat> getMerchantFeeSettlePolicyFloatList(
			PCPSWebInput webInput, Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "";
		// 平台机构管理员归属机构号为空
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = "select m.* from merchantfeesettlepolicyfloat m,merchantfeesettlementpolicy t where m.feepolicyid=t.feepolicyid and t.merchantno like '"
					+ issueCompanyNo + "%' order by FeePolicyId";
		} else {
			sql = "select m.* from MerchantFeeSettlePolicyFloat m order by FeePolicyId";
		}
		Query query = _em.createNativeQuery(sql,
				MerchantFeeSettlePolicyFloat.class).setFirstResult(
				startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public BigInteger getMerchantFeeSettlePolicyFloatTotal(PCPSWebInput webInput) {
		// 平台机构管理员归属机构号为空
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			Query query = _em
					.createNativeQuery("select nvl(count(*),0) from merchantfeesettlepolicyfloat m,merchantfeesettlementpolicy t where m.feepolicyid=t.feepolicyid and t.merchantno like '"
							+ issueCompanyNo + "%'");
			return new BigInteger(query.getSingleResult().toString());
		} else {
			Query query = _em
					.createNativeQuery("select nvl(count(*),0) from merchantfeesettlepolicyfloat m");
			return new BigInteger(query.getSingleResult().toString());
		}
	}

	@Override
	public void insertMerchantFeeSettlePolicyFloat(
			MerchantFeeSettlePolicyFloat merchantFeeSettlePolicyFloat)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, merchantFeeSettlePolicyFloat);
	}

	@Override
	public void updateMerchantFeeSettlePolicyFloat(
			MerchantFeeSettlePolicyFloat merchantFeeSettlePolicyFloat)
			throws PCPSApplicationException {
		MerchantFeeSettlePolicyFloat mfspf = getMerchantFeeSettlePolicyFloatByFeePolicyId(merchantFeeSettlePolicyFloat
				.getFeePolicyId());
		mfspf.setLimitAmount(merchantFeeSettlePolicyFloat.getLimitAmount());
		mfspf.setFloatFeeRatio(merchantFeeSettlePolicyFloat.getFloatFeeRatio());
		GeneralDao.update(_em, mfspf);
	}
}